var pool = require('../../config/mysql.connection');
var mapping = require('./mapping/user.mapping');
var redis = require('../../config/redis');
var pagerUtil = require('../utils/pager');

var redisData = mapping.redisData;
var redisSaveCount = 0;

module.exports = {
    removeUser:function(params,callback){
        pool.getConnection(function(err , connection){
            connection.query(mapping.removeUser,[params.ID],function(err,result){
                if(err){
                    callback(err);
                }
                callback(null,result);
                connection.release();
            });
        });
    },
    addUser:function(params,callback){
        pool.getConnection(function(err , connection){
            connection.query(mapping.addUser(params),function(err,result){
                if(err){
                    throw err;
                }
                callback(result);
                connection.release();
            });
        });
    },
    editUser:function(params,callback){
        pool.getConnection(function(err , connection){
            connection.query(mapping.updateUser(params),function(err,result){
                if(err){
                    throw err;
                }
                callback(result);
                connection.release();
            });
        });
    },
    getUserAll:function(params,callback){

        pool.getConnection(function(err , connection){
          connection.query(mapping.getUserCount,function(err,rows){
                if(err) throw err;

                var pagerObj = {
                    total : rows[0].COUNT,
                    currentPage : params.currentPage,
                    pageSize:params.pageSize
                };

                var page = pagerUtil.pager(pagerObj);

                params.start = page.start;
                params.end = page.end;

                connection.query(mapping.getUserAll(params) ,function(err,result){
                        if(err){
                            throw err;
                        }
                        if(result){
                           callback(result);
                        }
                        
                        connection.release();
                });
                  
          });
        });
    },
	getByUser:function(param , callback){
		pool.getConnection(function(err , connection){
          
          connection.query(mapping.getByUser , [param.userName,param.password],function(err,result){
                if(err){
                    throw err;
                }
                if(result && result[0]){
                    var userObj={
                        user:result[0]
                    };
                    connection.query(mapping.getUserRole , [userObj.user.ROLE_ID] , function(err , roleRes){
                        if(err){
                            throw err;
                        }
                        userObj.role = roleRes[0];
                        callback(null,userObj);
                    });
                }else{
                    callback(new Error('用户名或密码错误'));
                }
                
                connection.release();
          });
          
        });
	},
    setRedisNameData:function(name){
        for(var i in redisData){
            var obj = redisData[i];
            if(obj.name == name){
                pool.getConnection(function(err , connection){
                    connection.query(obj.sql ,function(err,result){
                        if(err){
                            throw err;
                        }
                        redis.set(obj.name,JSON.stringify(result),function(err,res){
                            if(err){
                                throw err;
                            }
                            console.log('【'+obj.name+'】'+'========列表存储成功....');
                        });
                        connection.release();
                    });
                });
                break;
            }
        }
    },
    setRedisData:function(){
        var _this = this;
        var data = redisData[redisSaveCount];
        pool.getConnection(function(err , connection){
          connection.query(data.sql ,function(err,result){
                if(err){
                    throw err;
                }
                if(result){
                    redis.set(data.name,JSON.stringify(result),function(err,res){
                        if(err){
                            throw err;
                        }
                        if(redisSaveCount < redisData.length-1){
                            console.log('【'+data.name+'】'+'========列表存储成功....');
                            redisSaveCount++;
                            _this.setRedisData();
                        }else{
                            redisSaveCount = 0;
                            //redis.end();
                            console.log('--------------结束存储redis列表----------------');
                        }
                    });
                }
                
                connection.release();
          });
          
        });
    }
};